package com.weixing.mall.provider.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AgentAuth;
import com.weixing.mall.core.support.auth.AgentInfo;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.provider.model.domain.AgentDeliveryAddress;
import com.weixing.mall.provider.model.dto.DeliveryDto;
import com.weixing.mall.provider.model.query.DeliveryQuery;
import com.weixing.mall.provider.model.vo.AgentDeliveryAddressVo;
import com.weixing.mall.provider.service.IAgentDeliveryAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 代理商收货地址
 *
 * @author Small
 * @since 1.0
 */
@RestController
@RequestMapping("/delivery")
public class AgentDeliveryAddressController extends BaseController {

    @Autowired
    private IAgentDeliveryAddressService agentDeliveryAddressService;


    /**
     * 加载代理商收货地址列表---客服调用
     * @param query 参数
     * @return 结果
     */
    @GetMapping("")
    public Result<IPage<AgentDeliveryAddressVo>> list(DeliveryQuery query) {
        IPage<AgentDeliveryAddressVo> iPage = agentDeliveryAddressService.selectOfPage(initMpPage(), query);
        return ResultUtil.success(iPage);
    }

    /**
     * 加载代理商收货地址列表---代理调用
     *
     * @param query 参数
     * @return    结果
     */
    @GetMapping("/agent")
    public Result<IPage<AgentDeliveryAddressVo>> listOfAgent(@AgentInfo AgentAuth auth, DeliveryQuery query) {
        query.setAgentId(auth.getAid());
        IPage<AgentDeliveryAddressVo> iPage = agentDeliveryAddressService.selectOfPage(initMpPage(), query);
        return ResultUtil.success(iPage);
    }

    /**
     * 删除代理商收货地址
     * @param id  id
     * @return  结果
     */
    @GetMapping("/del/{id}")
    public Result del(@PathVariable Long id) {
        agentDeliveryAddressService.removeById(id);
        return ResultUtil.success();
    }

    /**
     * 代理商收货地址详情
     * @param id id
     * @return 详情
     */
    @GetMapping("/detail/{id}")
    public Result<AgentDeliveryAddressVo> detail(@PathVariable Long id) {
        AgentDeliveryAddress byId = agentDeliveryAddressService.getById(id);
        AgentDeliveryAddressVo vo = new AgentDeliveryAddressVo();
        BeanUtils.copyProperties(byId, vo);
        return ResultUtil.success(vo);
    }


    /**
     * 编辑代理商收货地址
     *
     * @param deliveryDto 参数
     * @return  结果
     */
    @GetMapping("/edit")
    public Result edit(DeliveryDto deliveryDto) {
        AgentAuth auth = AuthUtil.getAgentExt();
        AgentDeliveryAddress address = new AgentDeliveryAddress();
        BeanUtils.copyProperties(deliveryDto, address);
        if (null == deliveryDto.getId()) {
            address.setAgentId(auth.getAid());
            agentDeliveryAddressService.save(address);
        } else {
            agentDeliveryAddressService.updateById(address);
        }
        return ResultUtil.success();
    }


}
